package middle.ground.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * swagger配置
 *
 * @author jrl
 * @author jrl
 * @date 2020-12-01
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	public static final String API_V1 = "/api";

	@Bean
	public Docket swaggerSpringMvcPlugin(Environment environment) {
		ParameterBuilder ticketPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<>();
		ticketPar.name("token")
				.description("token")
				.modelRef(new ModelRef("string"))
				.parameterType("query")
				.required(false).build();
		pars.add(ticketPar.build());

		return new Docket(DocumentationType.SWAGGER_2)
				.enable(getDocketEnable(environment))
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.build()
				.globalOperationParameters(pars);
	}

	/**
	 * 判断非prod环境下菜打开swagger
	 *
	 * @param * @param null:
	 * @return
	 */
	private boolean getDocketEnable(Environment environment) {
		String[] activeProfiles = environment.getActiveProfiles();
		return activeProfiles.length != 0 && !activeProfiles[0].contains("prod");
	}
}
